﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.Linq;

namespace DAL
{
    public class DaProduct : dBConnect
    {
        #region "Get all infomation in table Product............."
        public static List<ProductDto> GetAllProduct()
        {
            List<ProductDto> product = new List<ProductDto>();
            var result = from k in db.mp_SanPhams
                         select new ProductDto(k.ID_SP)
                         {
                             MaSP = k.MaSP,
                             TenSP = k.TenSP,
                             NhanHieu = new NhanHieuDto(int.Parse(k.ID_NhanHieu.ToString()), k.mp_NhanHieu.TenNhanHieu),
                             XuatXu = new XuatXuDto(int.Parse(k.ID_XuatXu.ToString()), k.mp_XuatXu.TenXuatXu),
                             Gia = Convert.ToDecimal(k.Gia),
                             DoiTuong = k.DoiTuong,
                             Size = k.Size,
                             TacDung = k.TacDung,
                             ChiTiet = k.ChiTiet,
                             HinhAnh = k.HinhAnh.ToString(),
                             CTPhanLoaiSp = new CTPhanLoaiSPDto(k.ID_CTPLSP, k.mp_CTPLSP.Ten_CTPLSP,new PLSanPhamDto(k.mp_CTPLSP.ID_PLSP,k.mp_CTPLSP.mp_PLSanPham.TenPLSP))
                         };
            product = result.ToList<ProductDto>();
            return product;
        }
        #endregion

        #region "Get one Product by ID..................."
        public static List<ProductDto> GetOneProductByID(ProductDto product)
        {
            var result = from k in db.mp_SanPhams
                         where (k.ID_SP == product.IdProduct)
                         select new ProductDto(k.ID_SP)
                         {
                             MaSP = k.MaSP,
                             TenSP = k.TenSP,
                             NhanHieu = new NhanHieuDto(int.Parse(k.ID_NhanHieu.ToString()), k.mp_NhanHieu.TenNhanHieu),
                             XuatXu = new XuatXuDto(int.Parse(k.ID_XuatXu.ToString()), k.mp_XuatXu.TenXuatXu),
                             Gia = Convert.ToDecimal(k.Gia),
                             DoiTuong = k.DoiTuong,
                             Size = k.Size,
                             HinhAnh = k.HinhAnh.ToString(),
                             CTPhanLoaiSp = new CTPhanLoaiSPDto(k.ID_CTPLSP, k.mp_CTPLSP.Ten_CTPLSP, null)
                         };
            return result.ToList<ProductDto>();
        }
        #endregion

        #region "Insert, Update for Product.............."
        public static string mp_Product_InsertUp(ProductDto product)
        {
            mp_SanPham o;
            int IdpProduct = product.IdProduct;
            if (IdpProduct<1)
            {
                o = new mp_SanPham();
            }
            else
            {
                o = db.mp_SanPhams.Where(j => j.ID_SP == product.IdProduct).SingleOrDefault();
            }
            if (!string.IsNullOrEmpty(product.MaSP))
                o.MaSP = product.MaSP;
            if (!string.IsNullOrEmpty(product.TenSP))
                o.TenSP = product.TenSP;
            if (!string.IsNullOrEmpty(product.NhanHieu.IDNhanHieu.ToString()))
                o.ID_NhanHieu = product.NhanHieu.IDNhanHieu;
            if (!string.IsNullOrEmpty(product.XuatXu.IDXuatXu.ToString()))
                o.ID_XuatXu = product.XuatXu.IDXuatXu;
            if (!string.IsNullOrEmpty(product.Gia.ToString()))
                o.Gia = Convert.ToDecimal(product.Gia);
            if (!string.IsNullOrEmpty(product.DoiTuong))
                o.DoiTuong = product.DoiTuong;
            if (!string.IsNullOrEmpty(product.Size))
                o.Size = product.Size;
            if (!string.IsNullOrEmpty(product.TacDung))
                o.TacDung = product.TacDung;
            if (!string.IsNullOrEmpty(product.ChiTiet))
                o.ChiTiet = product.ChiTiet;
            if (!string.IsNullOrEmpty(product.HinhAnh))
                o.HinhAnh = product.HinhAnh;
            if (!string.IsNullOrEmpty(product.CTPhanLoaiSp.IDChiTietPLSP.ToString()))
                    o.ID_CTPLSP = product.CTPhanLoaiSp.IDChiTietPLSP;
            if (IdpProduct<1)
            {
                db.mp_SanPhams.InsertOnSubmit(o);
            }
            db.SubmitChanges();
            return o.ID_SP.ToString();
        }
        #endregion

        #region "Delete one Product..............."
        public static void mp_SanPham_Del(ProductDto product, bool isPermanent)
        {
            var o = db.mp_SanPhams.Where(j => j.ID_SP == product.IdProduct).SingleOrDefault();
            if (o != null)
            {
                if (isPermanent)
                    db.mp_SanPhams.DeleteOnSubmit(o);
                db.SubmitChanges();
            }
        }
        #endregion
    }
}
